package com.netx.ebs;
import com.netx.data.EntityInstance;
import com.netx.generics.sql.Row;
import com.netx.generics.time.Moment;
import com.netx.generics.basic.IntegrityException;


public class Transgression extends EntityInstance {
	
	public enum TYPE {
		ILLEGAL_ACCESS,
		ILLEGAL_OPERATION
	};
	
	private final User _user;
	private final TYPE _type;
	private final String _requestURL;
	private final String _message;
	private final Moment _timeOcurred;
	
	Transgression(Row data, User u) {
		super(data.getLong(1));
		_user = u;
		char type = data.getChar(2);
		if(type == 'a') {
			_type = TYPE.ILLEGAL_ACCESS;
		}
		else if(type == 'o') {
			_type = TYPE.ILLEGAL_OPERATION;
		}
		else {
			throw new IntegrityException(type);
		}
		_requestURL = data.getString(3);
		_message = data.getString(4);
		_timeOcurred = data.getMoment(5);
	}

	public User getUser() {
		return _user;
	}

	public TYPE getType() {
		return _type;
	}
	
	public String getRequestURL() {
		return _requestURL;
	}
	
	public String getMessage() {
		return _message;
	}

	public Moment getTimeOccurred() {
		return _timeOcurred;
	}
	
	public String toString() {
		return "Transgression ("+_type+") by \""+_user.getUsername()+"\" @ "+getTimeOccurred().format()+": "+getMessage();
	}

}
